package com.cqupt.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.mapper.WordMapper;
import com.cqupt.model.constant.Constant;
import com.cqupt.model.pojo.Book;
import com.cqupt.model.pojo.Word;
import com.cqupt.model.vo.PageVo;
import com.cqupt.service.BookService;
import com.cqupt.mapper.BookMapper;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
        implements BookService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private WordMapper wordMapper;

    @Override
    public Book getBook(int bookId) {

        Book book = bookMapper.selectById(bookId);
        Integer userId = AuthContextUtil.get().getId();
        String json = stringRedisTemplate.opsForValue().get(userId + ":" + bookId);
        if (!StrUtil.isEmpty(json)) {
            return book;
        }
        List<Word> list = wordMapper.getByBookId(bookId);
        stringRedisTemplate.opsForValue().set(userId + ":" + bookId, JSON.toJSONString(list));
        return book;
    }

    @Override
    public List<Book> getAll() {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Book::getStatus, Constant.ENABLED_STATE);
        return bookMapper.selectList(wrapper);
    }

    @Override
    public PageVo<Book> getByCategoryId(int id, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Book::getCategoryId, id);
        wrapper.eq(Book::getStatus, Constant.ENABLED_STATE);
        PageVo<Book> pageVo = new PageVo<>();
        if (pageNum != null && pageSize != null) {
            Page<Book> page = new Page<>(pageNum, pageSize);
            List<Book> records = page(page, wrapper).getRecords();
            long pages = page.getPages();
            pageVo.setTotal(pages);
            pageVo.setRecords(records);
        } else {
            pageVo.setRecords(bookMapper.selectList(wrapper));
        }
        return pageVo;

    }

    @Override
    public List<Book> queryBook(String title) {


        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Book::getName, title);
        List<Book> books = bookMapper.selectList(wrapper);
        return books;

    }

    @Override
    public PageVo<Book> getBookByPage(Integer pageNum, Integer pageSize) {
        Page<Book> bookPage = new Page<>(pageNum, pageSize);

        List<Book> records = page(bookPage).getRecords();
        PageVo<Book> pageVo = new PageVo<>(bookPage.getTotal(), records);
        return pageVo;
    }
}




